<template>
  <div :class="{ hidden: hidden }" class="pagination-container">
    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :layout="layout"
      :page-sizes="pageSizes"
      :pager-count="pagerCount"
      :total="total"
      :prev-text="'上一页'"
      :next-text="'下一页'"
      v-bind="$attrs"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script setup lang="ts">
import { computed, ref, onMounted, onUnmounted } from 'vue'
import { scrollTo } from '@/utils/scroll-to'

interface Props {
  total: number
  page?: number
  limit?: number
  pageSizes?: number[]
  // 移动端页码按钮的数量端默认值5
  pagerCount?: number
  layout?: string
  background?: boolean
  autoScroll?: boolean
  hidden?: boolean
}

defineOptions({
  name: 'Pagination',
})

const props = withDefaults(defineProps<Props>(), {
  page: 1,
  limit: 20,
  pageSizes: () => [10, 20, 30, 50],
  pagerCount: document.body.clientWidth < 992 ? 5 : 7,
  layout: 'total, sizes, prev, pager, next',
  background: true,
  autoScroll: true,
  hidden: false,
})

// 监听窗口大小变化来动态设置pagerCount
const windowWidth = ref(document.body.clientWidth)
let resizeHandler: (() => void) | null = null

onMounted(() => {
  resizeHandler = () => {
    windowWidth.value = document.body.clientWidth
  }
  window.addEventListener('resize', resizeHandler)
})

onUnmounted(() => {
  if (resizeHandler) {
    window.removeEventListener('resize', resizeHandler)
  }
})

const pagerCount = computed(() => {
  if (props.pagerCount !== undefined) {
    return props.pagerCount
  }
  return windowWidth.value < 992 ? 5 : 7
})

const emit = defineEmits<{
  (e: 'update:page', value: number): void
  (e: 'update:limit', value: number): void
  (e: 'pagination', value: { page: number; limit: number }): void
}>()

const currentPage = computed({
  get: () => props.page || 1,
  set: (val) => emit('update:page', val),
})

const pageSize = computed({
  get: () => props.limit || 20,
  set: (val) => emit('update:limit', val),
})

const handleSizeChange = (val: number) => {
  if (currentPage.value * val > props.total) {
    currentPage.value = 1
  }
  emit('pagination', { page: currentPage.value, limit: val })
  if (props.autoScroll) {
    scrollTo(0, 800)
  }
}

const handleCurrentChange = (val: number) => {
  emit('pagination', { page: val, limit: pageSize.value })
  if (props.autoScroll) {
    scrollTo(0, 800)
  }
}
</script>

<style lang="scss" scoped>
.pagination-container {
  background: #fff;
  margin-top: 25px;
}
.pagination-container.hidden {
  display: none;
}
:deep(.el-pagination) {
  display: flex;
  align-items: center;
  .el-pagination__total {
    color: #808080;
    font-size: 16px;
  }
  .el-pagination__sizes {
    margin: 0 13px 0 0;
  }
  .el-input {
    width: 95px;
    height: 34px;
    margin: 0;
  }
  .el-input__inner {
    font-size: 16px;
    color: #808080;
    width: 95px;
    height: 34px;
    text-align: left;
    padding-right: 0 !important;
  }
  .el-select__caret {
    color: #808080;
  }
  .el-input__suffix {
    right: 6px;
    transform: scale(1);
    height: 34px;

    line-height: 34px;
  }
  span:not([class*='suffix']),
  button {
    height: 34px;
    line-height: 34px;
  }
}
</style>
